/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.math;

import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.Logging;
import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.OutputPort;

/**
 * MultiplicationNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class MultiplicationNode extends AbstractNode implements Logging {

	/**
	 * channels
	 */
	private int channels;

	/**
	 * MultiplicationNode Constructor
	 * 
	 */
	public MultiplicationNode() {
		this("MUL" + nr++, false);
	}

	/**
	 * MultiplicationNode Constructor
	 * 
	 * @param name
	 * @param continuous
	 */
	public MultiplicationNode(String name, boolean continuous) {
		this(name, 2, continuous);
	}

	/**
	 * @param name
	 */
	public MultiplicationNode(String name, int channels, boolean continuous) {
		super(name, continuous);
		this.channels = channels;
	}

	/**
	 * @see de.teamskill.pux.Node#compute()
	 */
	@Override
	public void compute() {
		if (!nodeReady()) {
			return;
		}
		LOG_NODE.finer(getNodeName() + " performing custom computation");
		double result = 1.00;
		for (InputPort ip : getInputPorts()) {
			String value = ip.fetchData().toString();
			Double val = Double.parseDouble(value);
			result *= val;
		}
		for (OutputPort op : getOutputPorts()) {
			LOG_NODE.finer(getNodeName() + " pushing result (" + result + ") to "
					+ op.getNode().getNodeName() + "[" + op.getName() + "]");
			op.pushData(new Double(result));
		}
		LOG_NODE.finest(getNodeName() + " finished with custom computation");
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		for (int i = 0; i < channels; i++) {
			addInputPort(new InputPort("IN" + (i + 1), this, true));
		}
		addOutputPort(new OutputPort("OUT", this));
	}

	/**
	 * JavaBean-style getter-method for channels
	 * 
	 * @return the channels
	 */
	public int getChannels() {
		return channels;
	}

	/**
	 * /** JavaBean-style setter-method for channels
	 * 
	 * @param channels
	 *            the channels to set
	 */
	public void setChannels(int channels) {
		this.channels = channels;
	}

}
